package org.erepublik.company.product;

import org.erepublik.company.Company;
import org.erepublik.company.ECountry;
import org.erepublik.company.EProductType;
import org.json.JSONException;
import org.json.JSONObject;

public class Weapons extends Product {

    public Weapons(EProductType productType) {
        super(productType);
    }

    public double getDamage() {
        return (int)getProductDesign().getAttributeValue(EProductAttribute.DAMAGE);
    }
    
    public int getDurability() {
        return getProductDesign().getAttributeValue(EProductAttribute.DURABILITY)/10;
    }
    
    public double getPricePerUse() {
        return Math.round( getPriceInGold()/(double)getDurability() * 10000.0d)/10000.0d;
    }
    
    public int getQuality() {
        int customizationPoints = 
                + getProductDesign().getAttributeValue(EProductAttribute.DAMAGE)
                + getProductDesign().getAttributeValue(EProductAttribute.DURABILITY);
        return customizationPoints/30;
    }
    
    @Override
    public JSONObject toJSONObject() throws JSONException {
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("companyOwner", getCompanyOwner().getId());
        jsonObj.put("companyCountry", getCompanyOwner().getCountry());
        jsonObj.put("companyName", getCompanyOwner().getName());
        jsonObj.put("market", getCountry().toString());
        jsonObj.put("price", getPrice());
        jsonObj.put("priceInGold", getPriceInGold());
        jsonObj.put("quantity", getQuantity());
        jsonObj.put("damage", getProductDesign().getAttributeValue(EProductAttribute.DAMAGE));
        jsonObj.put("durability", getProductDesign().getAttributeValue(EProductAttribute.DURABILITY));

        return jsonObj;
    }

    
    public static void getWeaponsFromJSON(JSONObject object, Weapons product) throws JSONException {
  
        String companyName = "no name";
        if (object.has("companyName"))
            companyName = (String)object.get("companyName");
        product.setCompanyOwner( new Company( (Integer)object.get("companyOwner"), 0, ECountry.valueOf( (String) object.get("companyCountry") ),companyName ));
        product.setMarket(ECountry.valueOf( (String) object.get("market") ) );
        if (object.get("price") instanceof Integer)
            product.setPrice( (Integer)object.get("price") );
        else
            product.setPrice( (Double)object.get("price") );
        if (object.get("priceInGold") instanceof Integer)
            product.setPriceInGold( (Integer)object.get("priceInGold") );
        else
            product.setPriceInGold( (Double)object.get("priceInGold") );
        product.setQuantity((Integer)object.get("quantity"));
        product.getProductDesign().setAttributeValue(EProductAttribute.DAMAGE, 
                (Integer)object.get("damage"));

        product.getProductDesign().setAttributeValue(EProductAttribute.DURABILITY, 
                (Integer)object.get("durability"));
        
    }

}
